<?php
namespace util;

use exception\SystemException;

class CurlUtil
{
    /**
     * @param $url
     * @param $data
     * @param $setting | array
     * @return mixed
     */
    public static function post($url, $data, $setting = []) {
        if(!isset($setting['header'])) {
            $setting['header'] = [];
        }
        if(!isset($setting['post_method'])) {
            $setting['post_method'] = 2;
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_POST, true);
        if($setting['post_method'] === 1) {
            // query-params
            $url = CurlUtil::toQueryParamsUrl($url, $data);
        } else if($setting['post_method'] === 2) {
            // x-www-form-urlencoded
            $data = http_build_query($data);
            $setting['header']['Content-Type'] = 'application/x-www-form-urlencoded';
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        } else if($setting['post_method'] == 'json') {
            $setting['header']['Content-Type'] = 'application/json';
            $data = json_encode($data);
            $setting['header']['Content-Length'] = strlen($data);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        } else {
            // form-data
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        if(isset($setting['CURLOPT_CUSTOMREQUEST']) && $setting['CURLOPT_CUSTOMREQUEST'] == 'put') {
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        if(isset($setting['response-header'])) {
            curl_setopt($curl, CURLOPT_HEADER, $setting['response-header'] === false);
        }
        if(isset($setting['header'])){
            $header = [];
            foreach ($setting['header'] as $key => $value) {
                array_push($header, "$key: $value");
            }
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        if(isset($setting['referer'])){
            curl_setopt($curl, CURLOPT_REFERER, $setting['referer']);
        }
        if(!isset($setting['agent'])){
            curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
        }
        if(isset($setting['cookie'])){
            curl_setopt($curl, CURLOPT_COOKIE, $setting['cookie']);
        }
        if(isset($setting['CURLOPT_SSLCERT'])) {
            curl_setopt($curl, CURLOPT_SSLCERT, $setting['CURLOPT_SSLCERT']);
        }
        if(isset($setting['CURLOPT_SSLKEY'])) {
            curl_setopt($curl, CURLOPT_SSLKEY, $setting['CURLOPT_SSLKEY']);
        }
        $output = curl_exec($curl);
        $errmsg = curl_error($curl);
        curl_close($curl);
        if($output == false) {
            throw new SystemException($errmsg);
        }
        return $output;
    }

    public static function get($url, $params= [], $setting = []){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        if(!empty($params)) {
            if($setting['post_method'] == 'json') {
                $setting['header']['Content-Type'] = 'application/json; charset=utf-8';
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
                curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
            } else {
                $url = CurlUtil::toQueryParamsUrl($url, $params);
            }
        }
        if(isset($setting['response-header'])) {
            curl_setopt($curl, CURLOPT_HEADER, $setting['response-header'] === false);
        }
        if(!isset($setting['agent'])){
            curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
        }
        if(isset($setting['header'])){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $setting['header']);
        }

        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    public static function toQueryParamsUrl($url, $array) {
        $url = $url.'?';
        foreach($array as $key => $value) {
            $url = $url . $key . '=' . $value . '&';
        }
        return $url;
    }
}